package com.wy.xinzhi.common.actions;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.exceptions.ServiceException;
import com.wy.xinzhi.common.constant.Constants;
import com.wy.xinzhi.common.pojos.THoliday;
import com.wy.xinzhi.common.services.HolidayService;
import com.wy.xinzhi.common.utils.DateUtil;

/**
 * 节假日管理
 * 
 * @author X
 * 
 */
@ParentPackage("json")
@Namespace(value = "/xz")
@SuppressWarnings({ "rawtypes", "unused" })
public class HolidayAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(HolidayAction.class);
	private PropertiesUtil p = new PropertiesUtil();

	// 是否成功
	private String jsonRes;

	private HolidayService holidayService;
	// 节假日列表
	private List<Map<String, Object>> holidays;

	/**
	 * 获取节假日信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@Action(value = "queryHolidays", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String queryHolidays() throws UnsupportedEncodingException {
		String year = ServletActionContext.getRequest().getParameter("year");
		holidays = holidayService.queryHolidays(year);
		if (holidays != null) {
			this.jsonRes = "true";
		} else {
			this.jsonRes = "false";
		}
		return SUCCESS;
	}

	/**
	 * 增加节假日信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException
	 */
	@Action(value = "addHoliday", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String addHoliday() throws UnsupportedEncodingException,
			ServiceException {
		String holiDate = ServletActionContext.getRequest().getParameter(
				"holiDate");
		String remark = ServletActionContext.getRequest()
				.getParameter("remark");
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		THoliday holiday = new THoliday();
		holiday.setHoliDate(DateUtil.stringToDate(holiDate,
				Constants.DateConstants.FOMATE_YYYYMMDD));
		holiday.setRemark(remark);
		holiday.setStoreId(Integer.parseInt(storeId));
		holiday.setCreateDate(new Timestamp(new Date().getTime()));
		// 需要确定获取用户信息后修改
		holiday.setUserId(1);
		holidayService.addHoliday(holiday);
		return SUCCESS;
	}

	/**
	 * 删除节假日信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException
	 */
	@Action(value = "deleteHoliday", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String deleteHoliday() throws UnsupportedEncodingException,
			ServiceException {
		String holiId = ServletActionContext.getRequest()
				.getParameter("holiId");
		THoliday holiday = new THoliday();
		holiday.setHoliId(Integer.parseInt(holiId));
		holidayService.deleteHoliday(holiday);
		return SUCCESS;
	}

	/**
	 * 修改节假日信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException
	 */
	@Action(value = "updateHoliday", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String updateHoliday() throws UnsupportedEncodingException,
			ServiceException {
		String holiId = ServletActionContext.getRequest()
				.getParameter("holiId");
		String holiDate = ServletActionContext.getRequest().getParameter(
				"holiDate");
		String remark = ServletActionContext.getRequest()
				.getParameter("remark");
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		THoliday holiday = new THoliday();
		holiday.setHoliId(Integer.parseInt(holiId));
		holiday.setHoliDate(DateUtil.stringToDate(holiDate,
				Constants.DateConstants.FOMATE_YYYYMMDD));
		holiday.setRemark(remark);
		holiday.setStoreId(Integer.parseInt(storeId));
		holiday.setCreateDate(new Timestamp(new Date().getTime()));
		// 需要确定获取用户信息后修改
		holiday.setUserId(1);
		holidayService.updateHoliday(holiday);
		return SUCCESS;
	}

}
